﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using PhoneServiceTest;

namespace 模拟话机
{
    class Program
    {
        static void Main(string[] args)
        {

            IPEndPoint ipAddr = new IPEndPoint(new IPAddress(new byte[] { 211, 140, 7, 31 }), 12122);
            //IPEndPoint localEndPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 12122);
            TcpClient clent = new TcpClient();
            clent.Connect(ipAddr);
            NetworkStream netStream = clent.GetStream();
            
            try
            {
            	var bytes = BuildPhoneAuthenMsg();
            	netStream.Write(bytes,0,bytes.Length);
				bytes= new byte[1024];
				netStream.Read(bytes,0,1024);

                PublicMethod.WiteBytesToFile("auth_response.txt",bytes);
                Console.Read();
            }
            catch (Exception ex) { }
        }
        /// <summary>
        /// 生成公话验证的内容
        /// </summary>
        /// <returns></returns>
        static byte[] BuildPhoneAuthenMsg()
        {
            byte[] buffer = new byte[36];
            string strLength = "0036";
            string strFunNO = "10";
            string strSeqNO = "0001";
            string StrDeviceID = "773023";
            Encoding.Default.GetBytes(strLength).CopyTo(buffer, 0);
            Encoding.Default.GetBytes(strFunNO).CopyTo(buffer, 4);
            Encoding.Default.GetBytes(strSeqNO).CopyTo(buffer, 6);
            Encoding.Default.GetBytes(StrDeviceID).CopyTo(buffer, 10);
            Encoding.Default.GetBytes("".PadLeft(8, ' ')).CopyTo(buffer, 18);
            return buffer;
        }
        /// <summary>
        /// 获取共话认证的请求内容
        /// </summary>
        /// <param name="bytes"></param>
        static void GetPhoneAuthenMsg(byte[] bytes)
        {
            string lenght = Encoding.Default.GetString(bytes, 0, 4);
            string funcNO = Encoding.Default.GetString(bytes, 4, 2);
            string seqNO = Encoding.Default.GetString(bytes, 6, 4);
            string flag = Encoding.Default.GetString(bytes, 10, 1);
        }
        /// <summary>
        /// 生成公话状态查询请求应答
        /// </summary>
        /// <param name="strSeqNO"></param>
        /// <returns></returns>
        static byte[] BuildABTStatusMsg(string strSeqNO)
        {
            string strLength = "0118";
            string strFuncNO = "82";
            string strDeviceId = "773023";
            string strVersionInfo = "VER1.00 2013/02/25";
            //string strMinitorInfo = "111";
            //string strMinitorInfoLast18 = "VER1.00 2013/02/25";
            return Encoding.Default.GetBytes(strLength)
                .Concat(Encoding.Default.GetBytes(strFuncNO))
                .Concat(Encoding.Default.GetBytes(strSeqNO))
                .Concat(Encoding.Default.GetBytes(strDeviceId))
                .Concat(Encoding.Default.GetBytes(strVersionInfo))
                .ToArray();
        }

        static string GetABTStatusMsg(byte[] bytes)
        {
            string lenght = Encoding.Default.GetString(bytes, 11, 4);
            string funcNO = Encoding.Default.GetString(bytes, 15, 2);
            string seqNO = Encoding.Default.GetString(bytes, 17, 4);
            string curTime = Encoding.Default.GetString(bytes, 21, 14);
            return seqNO;
        }

        static byte[] BuildStudentCardLoginMsg() 
        {
            string Device_msisdn = "15224101019";
            string DeviceID = "773023";
            string cardID = "";
            string studentID = "";
            string request_time = DateTime.Now.ToString("yyyyMMddHHmmss");

            return null;
        }
    }
}
